import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ToastController,Events} from 'ionic-angular';
import $ from 'jquery';

import {SideMenuService} from "../../../services/common/side-menu.service";
import {TabsPage} from "../../tabs/tabs";
import{MessageDetailProvider} from '../../../providers/information/message-detail/message-detail.provider';
import {MsgDetail} from '../../../model/pages/information/message-list/message-list.model';
import {ToastService} from '../../../services/common/toast.service';
import {LoadingService} from "../../../services/common/loading.service";
import {ThemeBrowserService} from '../../../services/native/theme-browser/theme-browser.service';

@IonicPage()
@Component({
    selector: 'page-message-detail',
    templateUrl: 'message-detail.html',
    providers:[
        SideMenuService,
        MessageDetailProvider,
        ToastService,
        ThemeBrowserService
    ]
})
export class MessageDetailPage {
    // 返回一级页面
    openRoot = (name, index) => this.openTab(name, index);
    openMenu(myEvent) {
        this.sideMenuService.openSideMenu(myEvent)
    }
    openTab(name, index) {
        this.navCtrl.popToRoot();
        this.tabsPage.openPage({name: name, index: index});
    }

    // 当前消息id
    private currentId:string;
    // 消息详情
    private detailData:MsgDetail=new MsgDetail();
    // 收藏状态
    private collectStatus:boolean;
    // 是否显示收藏按钮
    private collectBtnStatus:boolean;
    // 请求状态
    private requestStatus:boolean;
    // 收藏请求
    collectResquest:boolean=true;

    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                private sideMenuService: SideMenuService,
                private tabsPage: TabsPage,
                private messageDetailProvider:MessageDetailProvider,
                private toastCtrl:ToastController,
                private toastService:ToastService,
                private load: LoadingService,
                private event:Events,
                private themeBrowserService:ThemeBrowserService
                ) {
    }

    public backButtonHandle = () => this.backClickHandle();

    ionViewDidLoad() {
        this.currentId=this.navParams.get('id');
        this.navParams.get('status')?this.collectBtnStatus=false:this.collectBtnStatus=true;
        this.getMsgDetail(this.currentId);
        this.signLink($);
    }

    // 获取消息详情
    getMsgDetail(id){
        this.load.showLoading();
        this.messageDetailProvider.getMessageDetail(id).subscribe((res)=>{
            this.load.dismissLoading();
            this.detailData=res;
            this.detailData.status?this.collectStatus=true:this.collectStatus=false;
            this.requestStatus=true;
            console.log(this.detailData);
        },error=>{
            this.requestStatus=true;
            this.load.dismissLoading();

        });
    }

    // 消息收藏切换
    switchCollect(){
        if(!this.collectResquest){
            return false;
        }else{
            this.collectResquest=false;
        }
        if(this.collectStatus){
            this.cancelCollect(this.currentId);
        }else{
           this.collectInfo(this.currentId);
        }
    }

    // 收藏资讯
    collectInfo(id){
        this.messageDetailProvider.collectInfo(id).subscribe((res)=>{
            this.collectStatus=true;
            this.collectResquest=true;
        },error=>{
            this.collectResquest=true;
        });
    }

    // 取消收藏
    cancelCollect(id){
        this.messageDetailProvider.cancelCollect(id).subscribe((res)=>{
            this.collectStatus=false;
            this.collectResquest=true;
        },error=>{
            this.collectResquest=true;
        });
    }

    // a标签重新渲染完成
    renderEnd(flag){
        if(flag)this.signLink($);
    }



    // a标签链接
    signLink($){
        let $this=this;
        $(".info-bd a").on("click",function(e){
            e.stopPropagation();
            e.preventDefault();
            let link=$(this).attr('link');
            if(link){
                $this.themeBrowserService.openBrowser(link);
            }
            return false;
        })
    }

    // 返回
    backClickHandle() {
        this.event.publish('message deteil');
        this.navCtrl.pop();
    }
}
